import axios from "axios";
const commonUrl = "http://192.168.1.106:444/app-yijiu-backstage";
const commonUrlHttp = "http://192.168.1.106:444/app-yijiu-member";
// const commonUrl = "http://8.136.110.18:444//app-yijiu-backstage";
// const commonUrl = "https://api.gwyckj.com//app-yijiu-backstage";
import { MessageBox, Message } from 'element-ui'

// 登录请求方法
const loginreq = (method, url, params) => {
  let text = "";
  let a = 0;
  for (const key in params) {
    if (a == 0) {
      text = text + key + "=" + params[key];
    } else {
      text = text + "&" + key + "=" + params[key];
    }
    a++;
  }
  return axios({
    method: method,
    url: commonUrl + url + "?" + text,
    ders: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    data: params,
    traditional: true,
    transformRequest: [
      function (data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }

        return ret;
      }
    ]
  }).then(res => {
    if (res.data.code == 422) {
      localStorage.clear()
      location.reload()
    } else {
      return res.data
    }
  }
  );
};
// 通用post公用方法
const req = (method, url, params) => {
  console.log("params", params);
  return axios({
    method: method,
    url: commonUrl + url,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      Authorization: localStorage.getItem("logintoken")
    },
    data: params,
    traditional: true,
    transformRequest: [
      function (data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }

        return ret;
      }
    ]
  }).then(res => {
    if (res.data.code == 422) {
      localStorage.clear()
      location.reload()
    } else {
      return res.data
    }
  }
  );
};

const reqGet = (method, url, params) => {
  let text = "";
  let a = 0;
  for (const key in params) {
    if (a == 0) {
      text = text + key + "=" + params[key];
    } else {
      text = text + "&" + key + "=" + params[key];
    }
    a++;
  }
  return axios({
    method: method,
    url: commonUrl + url + "?" + text,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      Authorization: localStorage.getItem("logintoken")
    },
    data: params,
    traditional: true,
    transformRequest: [
      function (data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }

        return ret;
      }
    ]
  }).then(res => {
    if (res.data.code == 422) {
      localStorage.clear()
      location.reload()
    } else {
      return res.data
    }
  }
  );
};
const reqHttpGet = (method, url, params) => {
  let text = "";
  let a = 0;
  for (const key in params) {
    if (a == 0) {
      text = text + key + "=" + params[key];
    } else {
      text = text + "&" + key + "=" + params[key];
    }
    a++;
  }
  return axios({
    method: method,
    url: commonUrlHttp + url + "?" + text,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      Authorization: localStorage.getItem("logintoken")
    },
    data: params,
    traditional: true,
    transformRequest: [
      function (data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }

        return ret;
      }
    ]
  }).then(res => {
    if (res.data.code == 422) {
      localStorage.clear()
      location.reload()
    } else {
      return res.data
    }
  }
  );
};
export { loginreq, req, reqGet, reqHttpGet};
